//
//  MPMineTableViewController.m
//  SearchPeople
//
//  Created by YoloMao on 16/5/9.
//
//

#import "MPMineTableViewController.h"
#import "MPEditUserInfoTableViewController.h"
#import "MPLoginViewController.h"
#import "MPMineTableViewCell.h"
#import "MPImport.h"

@interface MPMineTableViewController ()

@property (nonatomic, getter=isLogin) BOOL login;

@end

@implementation MPMineTableViewController

#pragma mark - UIViewController Life Cycle

- (void)viewDidLoad
{
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.login = [[MPSettingsManager getInstance].userDefault objectForKey:@"user"] ? YES : NO;
    [self.tableView reloadData];
}

#pragma mark - tableView Datasource & Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isLogin) return 3;
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.isLogin)
    {
        MPMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPMineTableViewCell"];
        cell.textLabel.text = @"登录";
        return cell;
    }
    else
    {
        if (indexPath.row == 0)
        {
            MPMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPMineTableViewCell"];
            cell.textLabel.text = @"个人资料设置";
            return cell;
        }
        if (indexPath.row == 1)
        {
            MPMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPMineTableViewCell"];
            cell.textLabel.text = @"App设置";
            return cell;
        }
        else
        {
            MPMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPMineTableViewCell"];
            cell.textLabel.text = @"退出当前账号";
            return cell;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.isLogin)
    {
        MPLoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"MPLoginViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    }
    else if (indexPath.row == 2)
    {
        [[MPSettingsManager getInstance].userDefault setObject:nil forKey:@"user"];
        self.login = NO;
        [self.tableView reloadData];
    }
    else if (indexPath.row == 0)
    {
        MPEditUserInfoTableViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"MPEditUserInfoTableViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:@"暂未开放"];
    }
}

@end